package gui;

import gui.events.WarenEingabeEvent;
import gui.interfaces.EingabeListener;
import gui.interfaces.WarenEingabeListener;

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Dimension;
import java.util.ArrayList;

import javax.swing.BorderFactory;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;
import javax.swing.event.EventListenerList;

import datatypes.Ware;

public class EingabeWarenPanel extends JPanel {

	private static final long serialVersionUID = 8209719334385753780L;
	private EventListenerList 	listenerList = new EventListenerList();
	
//	private JScrollPane					scroller;

	private JList<String>				warenListeBox;
	private ArrayList<Ware>				warenArrayList;
	private String[]					warenArray;

	public EingabeWarenPanel(ArrayList<Ware> alleWaren){
		setPreferredSize(new Dimension(700 , 550));
		setLayout(new CardLayout(2, 2));
		
		initComponents(alleWaren);
		initListeners();
		
	}
	
	private void initComponents(ArrayList<Ware> alleWaren) {
		warenArrayList = new ArrayList<Ware>();
		warenArrayList = alleWaren;
		warenArray = new String[alleWaren.size()];
		
		if (warenArrayList.size() == warenArray.length) {
			for (int i = 0; i < warenArray.length; i++) {
				warenArray[i] = warenArrayList.get(i).getName();
			}
		} else {
			System.out.println("fehler in wareneingabepanel: array != arraylist von waren");
		}
		
		warenListeBox = new JList<String>(warenArray);
		warenListeBox.setDragEnabled(false);
		warenListeBox.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
		warenListeBox.setLayoutOrientation(JList.VERTICAL_WRAP);
		warenListeBox.setPreferredSize(new Dimension(450, 200));
		warenListeBox.setVisibleRowCount(-1);
		warenListeBox.setBorder(BorderFactory.createTitledBorder("Wareneingabe"));
		warenListeBox.setFixedCellWidth(170);
		
//		scroller = new JScrollPane(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
//		scroller.setViewportView();
//		scroller.setBorder(null);
		this.add(warenListeBox, BorderLayout.CENTER);
	}
	
	private void initListeners() {

	}
	
	public void fireWarenEingabeEvent(WarenEingabeEvent event){
		Object[] listeners = this.listenerList.getListenerList();
		
		for (int i = 0; i < listeners.length; i += 2) {
			if (listeners[i] == EingabeListener.class) {
				((WarenEingabeListener)listeners[i+1]).warenEingabeEventOccurred(event);
			}
		}
	}

	public void addWarenEingabeListener(WarenEingabeListener listener){
		this.listenerList.add(WarenEingabeListener.class, listener);
	}

	public void removeWarenEingabeListener(WarenEingabeListener listener){
		this.listenerList.remove(WarenEingabeListener.class, listener);
	}

	public JList<String> getWarenListeBox() {
		return warenListeBox;
	}
	
	public void resetAuswahl(){
		this.warenListeBox.clearSelection();
	}
}